package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.R;
import com.example.entity.Dish;
import com.example.entity.ShoppingCart;
import com.example.service.ShoppingCartService;
import com.example.service.impl.DishServiceImpl;
import com.example.service.impl.ShoppingCartServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 购物车(ShoppingCart)表控制层
 *
 * @author makejava
 * @since 2022-05-26 18:41:36
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    /**
     * 服务对象
     */
    @Resource
    private ShoppingCartService shoppingCartService;
    @Resource
    private DishServiceImpl dishService;
    @GetMapping("/list")
    public R<List<ShoppingCart>> getshoppingcart() {
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }

    @PostMapping("/add")
    public R<String> addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        int a = 0;
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, session.getAttribute("user"));
        queryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId, shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
        if (shoppingCart1 != null) {
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartService.updateById(shoppingCart1);
            return R.success("加入成功");
    }
        shoppingCart.setCreateTime(new Date());
        shoppingCart.setUserId((Long)session.getAttribute("user"));
        shoppingCart.setNumber(1);
        shoppingCartService.save(shoppingCart);
        return R.success("加入成功");
}

    @DeleteMapping("/clean")//清空购物车
    public R<String> deleteClean(HttpSession session) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, session.getAttribute("user"));
        boolean a = shoppingCartService.remove(queryWrapper);
        if (a) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败出现异常");
        }

    }
    @PostMapping("/sub")
    public R<String>subShopping(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        LambdaQueryWrapper<ShoppingCart>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        queryWrapper.eq(ShoppingCart::getUserId,session.getAttribute("user"));
        ShoppingCart shoppingCart1=shoppingCartService.getOne(queryWrapper);
        shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
        if (shoppingCart1.getNumber()==0){
            shoppingCartService.removeById(shoppingCart1);
            return R.success("去除成功");
        }
        shoppingCartService.updateById(shoppingCart1);

        return R.success("去除成功");
    }
}